﻿@using System.Collections.Generic
@model List<HuntingFishGame.ManageDomain.Model.User.WxCodeModel>
@{
    ViewBag.Title = "修改密码";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<form action="#">
    <div class="tbox">
        <table width="100%" class="formTable">
            <tbody>
                <tr>
                    <td>
                        <input type="radio" name="PW" id="pw1" value="1" onclick="changeType('pw1')">
                        <label for="pw1" class="checkbox">修改登录密码</label>
                    </td>
                    <td>
                        <input type="radio" name="PW" id="pw2" value="0" onclick="changeType('pw2')">
                        <label for="pw2" class="checkbox">修改银行密码</label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <select id="UserId">
                            <option value="">请选择</option>
                            @if (Model != null && Model.Any())
                            {
                                foreach (var item in Model)
                                {
                                    <option value="@item.UserId">@item.NickName</option>
                                }
                            }
                        </select>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input id="OldPwd" type="password" placeholder="原密码" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input id="NewPwd" type="password" placeholder="新密码" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input id="NewPwdConfirm" type="password" placeholder="确认新密码" />
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</form>
<div class="btns">
    <input id="updateLoginSubmit" type="submit" class="btnBlue w80" value="修改密码" onclick="UpdateLoginSubmit()" />
    <input id="updateBankSubmit" type="button" class="btnBlue w80" value="修改密码" onclick="UpdateBankSubmit()" />
</div>

<script src="~/Scripts/jquery.js"></script>
<script type="text/javascript">
    function changeType(tempid) {
        //var tempobj = $('input:radio[name="PW"]:checked');
        if (tempid === "pw1") {
                $("#updateLoginSubmit").show();
                $("#updateBankSubmit").hide();
            } else {
                $("#updateLoginSubmit").hide();
                $("#updateBankSubmit").show();
        }
        $('#' + tempid).attr("checked", "checked");
    }
    $(document).ready(function() {
        changeType('pw1');
    });
    function checkForm() {
        var $UserId = $("#UserId");
        var $password = $("#OldPwd");
        //验证用户名为空
        if ($UserId.val().toString().trim().length <= 0) {
            alert("请选择绑定账号");
            $UserId.focus();
            return false;
        }
        //验证密码为空
        if ($password.val().toString().trim().length <= 0) {
            alert("请输入原密码");
            $password.focus();
            return false;
        }
        //验证密码长度不能小于6
        if ($password.val().toString().trim().length < 6 || $password.val().toString().trim().length > 20) {
            alert("用户名或密码错误");
            $password.focus();
            return false;
        }
        if ($("#NewPwd").val().toString().trim().length < 6 || $("#NewPwd").val().toString().trim().length > 20 || $("#NewPwdConfirm").val().toString().trim().length < 6 || $("#NewPwdConfirm").val().toString().trim().length > 20) {
            alert("密码由6-20个英文字母和数字组成");
            $("#NewPwd").focus();
            return false;
        }
        if ($("#NewPwd").val().toString().trim() !== $("#NewPwdConfirm").val().toString().trim()) {
            alert("重复密码不一致");
            return false;
        }
        return true;
    }
    //登录提交
    function UpdateLoginSubmit() {
        var ret = checkForm();
        if (!ret) {
            return;
        }
        //验证通过，提交登录
        $.ajax({
            url: "/UserPassWord/UpdateLoginPwd?@Html.Raw(Request.QueryString.ToString())",
            data: {
                'UserId': $("#UserId").val().toString().trim(),
                'OldPwd': $("#OldPwd").val().toString().trim(),
                'NewPwd': $("#NewPwd").val().toString().trim()
            },
            type: "POST",
            success: function (data) {
                //提交成功直接转入地址
                if (data.code === 1) {
                    if (data.ReturnUrl === "" || data.ReturnUrl == null) {
                        window.location.href = "@SiteUrls.Instance().Success()";
                    } else {
                        window.location.href = decodeURIComponent(data.ReturnUrl);
                    }
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("系统发生未知错误");
            }
        });
    }

    //注册检测
    function UpdateBankSubmit() {
        var ret = checkForm();
        if (!ret) {
            return;
        }
        $.ajax({
            url: "/UserPassWord/UpdateBankSubmit?@Html.Raw(Request.QueryString.ToString())",
            data: {
                'UserId': $("#UserId").val().toString().trim(),
                'OldPwd': $("#OldPwd").val().toString().trim(),
                'NewPwd': $("#NewPwd").val().toString().trim()
            },
            type: "POST",
            success: function (data) {
                //提交成功直接转入地址
                if (data.code === 1) {
                    if (data.ReturnUrl === "" || data.ReturnUrl == null) {
                        window.location.href = "@SiteUrls.Instance().Success()";
                    } else {
                        window.location.href = decodeURIComponent(data.ReturnUrl);
                    }
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("服务器开小差了，请重试");
            }
        });
    }
</script>